//package com.qf.dao.Impl;
//
//import com.qf.dao.GoodsDao;
//import com.qf.domain.Goods;
//import com.qf.utils.DataSourceUtil;
//import org.apache.commons.dbutils.QueryRunner;
//import org.apache.commons.dbutils.handlers.BeanHandler;
//import org.apache.commons.dbutils.handlers.BeanListHandler;
//import org.apache.commons.dbutils.handlers.ScalarHandler;
//
//import java.sql.SQLException;
//import java.util.List;
//
//public class GoodsDaoImpl implements GoodsDao {
//    QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
//    @Override
//    public long findTotalCount(int typeId) {
//        String sql = " select count(0) from tb_goods where typeid = ? ";
//        try {
//            Long query = (Long) queryRunner.query(sql, new ScalarHandler(), typeId);
//            return query;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//    @Override
//    public List<Goods> findData(int typeId, int i, int pageSize) {
//        String sql = " select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where typeid = ? limit ?, ? ";
//        try {
//            List<Goods> query = queryRunner.query(sql, new BeanListHandler<>(Goods.class), typeId, i, pageSize);
//            return query;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    @Override
//    public Goods findById(int id) {
//        String sql = " select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where id = ? ";
//        try {
//            Goods goods = queryRunner.query(sql, new BeanHandler<>(Goods.class), id);
//            return goods;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}

package com.qf.dao.Impl;
import com.qf.dao.GoodsDao;
import com.qf.domain.Goods;

import com.qf.utils.DataSourceUtil;
import com.sun.javafx.binding.StringFormatter;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class GoodsDaoImpl implements GoodsDao {
    @Override
    public long findTotalCount(int typeId) {
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select count(0) from tb_goods where typeid = ?";
        try {
            long count = (long)qf.query(sql , new ScalarHandler(), typeId);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public List<Goods> findData(int typeId, int startIndex, int pageSize) {
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where typeid = ? limit ?, ?";
        try {
            List<Goods> goodsList = qf.query(sql , new BeanListHandler<>(Goods.class), typeId, startIndex, pageSize);
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Goods findById(int id) {
        QueryRunner qf = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id,name,pubdate,picture,price,star,intro,typeid from tb_goods where id = ?";
        try {
            Goods goods  = qf.query(sql , new BeanHandler<>(Goods.class), id);
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Goods> findAllGoos() {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = " SELECT `id`,`name`,`price`,`pubdate`,`typeid` FROM `tb_goods` ";
        try {
            List<Goods> query = queryRunner.query(sql, new BeanListHandler<>(Goods.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败!!");
        }
        return null;
    }

    @Override
    public void addGoods(Goods goods) {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = " INSERT INTO `tb_goods` (id,`name`,`pubdate`,`picture`,`price`,`star`,`intro`,`typeid`) VALUE (null,?,?,?,?,?,?,?) ";
        try {
            queryRunner.update(sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),
                    goods.getStar(),goods.getIntro(),goods.getTypeid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败!!");
        }
    }

    @Override
    public void update(String name, double price, String time, int typeid, String info, int id) {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = " UPDATE `tb_goods` SET `NAME`= ? ,`price`= ?,`pubdate` =?, `typeid`=?,`intro`=?  WHERE id =? ";
        try {
            queryRunner.update(sql,name,price,time,typeid,info,id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("更新失败!!");
        }
    }
}

